package cn.jy.machine.util;

import cn.jy.common.machine.domain.ExpressBoxes;
import java.util.List;

public class BoxCalculator {

    // 计算箱格尺寸并分配位置信息
    public static void calculateBoxDimensionsAndPosition(List<ExpressBoxes> boxes, int totalWidth, int totalHeight, int totalDepth) {

        double largeBoxWidth = totalWidth * 2.5 / 5.0;
        double mediumBoxWidth = totalWidth * 1.5 / 5.0;
        double smallBoxWidth = totalWidth / 5.0;

        // 计算大箱格的数量
        int largeBoxCount = (int) Math.ceil((double) boxes.size() / (2.5 * 5 / 5.0));
        int mediumSmallBoxCount = boxes.size() - largeBoxCount * 5;

        int currentRow = 1;
        int currentCol = 1;

        for (int i = 0; i < boxes.size(); i++) {
            ExpressBoxes box = boxes.get(i);
            box.setPosition(String.format("%d行%d列", currentRow, currentCol));

            switch (box.getIsType()) {
                case 2: // 大箱
                    box.setWidth((int) largeBoxWidth);
                    box.setHeight(totalHeight / 2);
                    break;
                case 1: // 中箱
                    box.setWidth((int) mediumBoxWidth);
                    box.setHeight(totalHeight / 3);
                    break;
                case 0: // 小箱
                default:
                    box.setWidth((int) smallBoxWidth);
                    box.setHeight(totalHeight / 4);
                    break;
            }

            box.setDepth(totalDepth / (4 * 5));

            // 更新位置信息
            if (++currentCol > 5) {
                currentCol = 1;
                currentRow++;
            }
        }
    }
}
